package de.wojenko.spe;

import java.io.IOException;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.TextArea;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.AnchorPane;
import de.wojenko.sps.BacklogItemArtefact;

public class BacklogItemArtefactController extends AnchorPane {

	@FXML private TitledPane titledPane;
	@FXML private TextArea text;
	@FXML private TextArea comment;
	
	private BacklogItemArtefact card;
	
	public BacklogItemArtefactController(BacklogItemArtefact card) {
		
	}
	
	public BacklogItemArtefactController(String name, final BacklogItemArtefact card) {
		this.card = card;
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("BacklogItemArtefact.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);

        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }
        
        titledPane.setText(name);
        
        text.setText(this.card.getText());
        text.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(final ObservableValue<? extends String> observable, final String oldValue, final String newValue) {
            	card.setText(newValue);
            }
        });
        comment.setText(this.card.getComment());
        comment.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(final ObservableValue<? extends String> observable, final String oldValue, final String newValue) {
            	card.setComment(newValue);
            }
        });
	}
}
